﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Anyask
{
    public partial class room : Form
    {
        private delegate void UpdateListCallback(string strMessage);

        private net netInstance = net.Instance;
        private List<String> cachedMessages = new List<String>();

        public room()
        {
            //netInstance = net.Instance;
            netInstance.RaiseDataReceivedEvent += HandleDataRecieve;
            InitializeComponent();
        }


        public void HandleDataRecieve(object sender, NetMessageEventArgs e)
        {
            System.Diagnostics.Debugger.Log(0, null, e.Message);
            this.Invoke(new UpdateListCallback(this.UpdateList), new Object[] { e.Message });
            //MessageBox.Show(e.Message);
            //this.richTextBox1.Text = e.Message.ToString();// +"\n" + richTextBox1.Text;
        }

        public void UpdateList(string message)
        {
            //MessageBox.Show(richTextBox1.Text);
            String timeString = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
            String[] ps = new String[] { "Solomon", message, timeString };
            this.OutputBox.Document.InvokeScript("sendMessage", ps);
            //this.textBox1.Text += message;
        }

        private void ButtonSend_Click(object sender, EventArgs e)
        {
            this.sendMessage();
        }

        public void sendMessage()
        {
            String tempText = this.InputBox.Text;

            netInstance.send(tempText);
            
            this.InputBox.Text = "";

            if (tempText.Length == 0) return;

            cachedMessages.Add(tempText);
            try
            {
                String timeString = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
                String[] ps = new String[] { "Solomon", tempText, timeString };
                this.OutputBox.Document.InvokeScript("sendMessage", ps);
                //this.OutputBox.Document.InvokeScript("alert", new String[] { "Wolf" });
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            this.InputBox.Focus();
        }

        /*public void recieveCallback(Object sender, String str)
        {
            Debug.WriteLine(str);
            cachedMessages.Add(str);
            try
            {
                String timeString = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
                String[] ps = new String[] { "Solomon", str, timeString };

                DataRecieveHandler myRecieveHandler = new DataRecieveHandler(recieveCallback);

                //MessageBox.Show("baidu : "+str);
                //this.roomListView.Items.Add("wolf");
                this.Invoke(

                //MessageBox.Show(this.OutputBox.DocumentText.ToString());
                //.Show(str);
                //OutputBox.Document.InvokeScript("alert", ps);
                //this.OutputBox.Document.InvokeScript("sendMessage", new String[] { "Solomon", "WOLF IS CODING!", "test" });
                //this.OutputBox.Document.InvokeScript("alert", null);
                //this.OutputBox.DocumentText = str;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

        }*/

        private void room_Load(object sender, EventArgs e)
        {
            this.whenOffline();
            this.OutputBox.Navigate(Properties.Settings.Default.CHAT_CONTENT_DEFAULT_URL);
            
            roomListView.View = View.SmallIcon;
            //roomListView.LargeImageList = View.LargeIcon;
            for (int i=0; i<5; i++)
            {
                roomListView.Items.Add("用户用户用户用户" + i);
                //roomListView.Items.Add(new ListViewItem();
            }

            //netInstance.setRecieveCallback(this.recieveCallback);
        }

        private void room_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
            this.Owner.Show();
            this.Owner.Refresh();
        }

        private void roomListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            String chatUserName = roomListView.SelectedItems[0].Text;
            int chatUserListIndex = roomListView.SelectedItems[0].Index;
            Form form = new chat();
            form.Text = chatUserName + " - AnyAsk";
            form.Show();
        }

        private void chatInputSpliter_Click(object sender, EventArgs e)
        {
            InputBox.Focus();
        }

        private void OutputBox_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            whenOnline();
        }
        
        private void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.S && e.Alt == true) || (e.KeyCode == Keys.Enter && (e.Control != true && e.Shift != true)))
            {
                this.sendMessage();
                e.SuppressKeyPress = true;
            }
        }

        private void whenOffline()
        {
            InputBox.Enabled = false;
            ButtonSend.Enabled = false;
        }

        private void whenOnline()
        {
            InputBox.Enabled = true;
            ButtonSend.Enabled = true;
            this.InputBox.Focus();
        }
    }
}
